/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.api;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.metadata.io.R;
import cn.hallele.infrastructure.metadata.io.Response;
import cn.hallele.mc.sdk.usercenter.api.UserApi;
import cn.hallele.mc.sdk.usercenter.common.constants.ReqPathConst;
import cn.hallele.mc.sdk.usercenter.dto.UserDTO;
import cn.hallele.mc.sdk.usercenter.dto.UserQueryDTO;
import cn.hallele.ms.infrastructure.auditlog.annotation.ApiLog;
import cn.hallele.ms.usercenter.das.UserDBDas;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/10/5 14:15
 */
@RestController
@RequestMapping(ReqPathConst.USER_V1)
public class UserController implements UserApi {
    @Autowired
    private UserDBDas userDBDas;

    /**
     * 条件分页查询用户
     *
     * @param condition 查询条件
     * @return 用户
     */
    @Override
    @ApiLog(id = "USER_V1_LIST_USER")
    public Response<Page<UserDTO>> listUser(@Valid UserQueryDTO condition) {
        Page<UserDTO> result = userDBDas.listUser(condition);

        // password需要去掉
        if (result.getData() != null) {
            result.getData().forEach(u -> u.setPassword(null));
        }
        return R.ok(result);
    }

    /**
     * 获取用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    @ApiLog(id = "USER_V1_GET_USER")
    public Response<UserDTO> getUser(String username) {
        UserDTO result = userDBDas.getUser(username);
        Optional.ofNullable(result).ifPresent(u -> u.setPassword(null));
        return R.ok(result);
    }

    /**
     * 登录时候要获取的用户信息
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    @ApiLog(id = "USER_V1_GET_LOGIN_USER")
    public Response<UserDTO> getLoginUser(String username) {
        UserDTO result = userDBDas.getUser(username);
        return R.ok(result);
    }

    /**
     * 添加用户
     *
     * @param userDTO 用户信息
     * @return true：成功， false：失败
     */
    @Override
    @ApiLog(id = "USER_V1_ADD_USER")
    public Response<Boolean> addUser(@Valid UserDTO userDTO) {
        userDBDas.addUser(userDTO);
        return R.ok(true);
    }

    /**
     * 修改用户
     *
     * @param userDTO 用户
     * @return 修改后的用户信息
     */
    @Override
    @ApiLog(id = "USER_V1_UPDATE_USER")
    public Response<UserDTO> updateUser(@Valid UserDTO userDTO) {
        UserDTO result = userDBDas.updateUser(userDTO);
        return R.ok(result);
    }

    /**
     * 删除用户
     *
     * @param username 用户名
     * @return true：成功， false：失败
     */
    @Override
    @ApiLog(id = "USER_V1_DELETE_USER")
    public Response<Boolean> deleteUser(String username) {
        userDBDas.deleteUser(username);
        return R.ok(true);
    }

    /**
     * 修改密码
     *
     * @param username    用户名
     * @param oldPassword 旧密码，加密的
     * @param newPassword 新密码，加密的
     * @return true：成功， false：失败
     */
    @Override
    @ApiLog(id = "USER_V1_RESET_PASSWORD")
    public Response<Boolean> resetPassword(String username, String oldPassword, String newPassword) {
        Boolean result = userDBDas.resetPassword(username, oldPassword, newPassword);
        return R.ok(result);
    }

    /**
     * 获取用户头像
     *
     * @param username 用户名
     * @return 用户头像
     */
    @Override
    @ApiLog(id = "USER_V1_GET_AVATAR")
    public Response<String> getAvatar(String username) {
        String result = userDBDas.getAvatar(username);
        return R.ok(result);
    }
}
